package com.example.androidbighomework;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidbighomework.SQLiteHelper.MyHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    EditText et_account,et_password;
    Button bt_login,btn_to_zhuce;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();

    }

    private void init() {
        et_account=findViewById(R.id.ed_account);
        et_password=findViewById(R.id.ed_password);
        bt_login=findViewById(R.id.btn_login);
        btn_to_zhuce=findViewById(R.id.btn_to_zhuce);
        btn_to_zhuce.setOnClickListener(this);
        bt_login.setOnClickListener(this);
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode==1&&resultCode==2){
//            String accountData=data.getStringExtra("accountData");
//            Toast.makeText(this,accountData,Toast.LENGTH_LONG).show();
//        }
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                if (TextUtils.isEmpty(et_account.getText().toString().trim())){
                    Toast.makeText(this, "请输账号", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(et_password.getText().toString().trim())){
                    Toast.makeText(this, "请输密码", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    String account = et_account.getText().toString().trim();
                    int password = Integer.parseInt(et_password.getText().toString().trim());
                    if (find(account)!=-1){
                        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();

                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putInt(MainActivity.ACCOUNT,find(account));
                        intent.putExtras(bundle);
                        startActivity(intent);
                        startActivityForResult(intent,1);
//                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
//                        Bundle bundle=getIntent().getExtras();
//                        String account1=bundle.getString(account);
//                        bundle.putString(MainActivity.ACCOUNT,et_account.getText().toString().trim());
//                        intent.putExtras(bundle);
//                        startActivity(intent);
//                        startActivityForResult(intent,1);


                        //finish();
                    }
                    else {
                        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
                    }

                }
                break;
            case R.id.btn_to_zhuce:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    //根据账号查找
    public int find(String account){
        MyHelper helper=new MyHelper(LoginActivity.this);
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor cursor=db.query("user",null,"account=?",new String[]{account+""},null,null,null);
        if(cursor.getCount()!=0){
            while (cursor.moveToNext()){
                Integer uid=cursor.getInt(cursor.getColumnIndex("u_id"));
                String Account=cursor.getString(cursor.getColumnIndex("account"));
                String Password=cursor.getString(cursor.getColumnIndex("password"));
                Log.i("Account",Account);
                if(et_password.getText().toString().trim().equals(Password)){
                    Log.i("Password",Password);
                    return uid;
                }

            }

        }
        cursor.close();
        db.close();
        return -1;
    }
}